/**
 * Created by John on 2016/4/16.
 */

window.onerror = function(msg,file,line){
    alert("msg:"+msg+";file:"+file+";line:"+line);
};
$(document).ready (function(){
    radio_ware_operate();
    product_name();
    //品名操作
    $("#procucts_name").on("blur",function(){
        product_name();
    });
    //新增商品操作
    $("#addproduct").on("click", function () {
        checkContactNumber();
        addproduct();
    });
    //修改商品操作
    $("#editproduct").on("click", function () {
        checkContactNumber();
        updateproduct();
    });
    //库存操作
    $("#submit").on("click",function(){
        wareoperate();
    });
    //库存取消操作
    $("#reset").on("click",function(){
        warereset();
    });
    $("input[type='file']").change(function(){
        showpic(this.files);
    });
});
//telphoone format
function checkContactNumber() {
    var mobile = $.trim($("input[type='tel']").val());
    var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
    var isPhone = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
    //如果为1开头则验证手机号码
    if (mobile.substring(0, 1) == 1) {
        if (!isMobile.exec(mobile) && mobile.length != 11) {
            telwarn();
            return false;
        }
    }
    //如果为0开头则验证固定电话号码
    else if (mobile.substring(0, 1) == 0) {
        if (!isPhone.test(mobile)) {
            telwarn();
            return false;
        }
    }
    //否则全部不通过
    else {
        telwarn();
        return false;
    }
}
function telwarn(){
    $("#warning1").text("请输入正确的手机电话格式18888888888或固定电话格式0xxx-88888888");
}
//procuct_name event
function product_name(){
    var product_name=$("procucts_name").val();
    product_name_data={
        productname:product_name
    };
    product_name_url="品名匹配接口";
   postAjax(product_name_data,product_name_url,function(data){

   })
}
//addproduct
function addproduct(){
    var productname=$("#procucts_name").val();var serinum=$("#serinum").val();
    var productsize=$("#productsize").val();var material=$("#material").val();
    var unit=$("input[name='unit']:checked").val();
    var productstyle=$("#productstyle").val();
    var productcolor=$("#productcolor").val();var sex=$("input[name='sex']:checked").val();
    var productorgin=$("#productorgin").val();var retailprice=$("#retailprice").val();
    var productnote=$("#productnote").val();
    var newproductdata={
        name:productname,serinum:serinum,size:productsize,
        material:material,unit:unit,style:productstyle,
        color:productcolor,sex:sex,orgin:productorgin,
        retailprice:retailprice,note:productnote
    };
    var newproducturl="新增商品接口";
    postAjax(newproductdata,newproducturl, function () {

    })
}
//updateproduct
function updateproduct(){
    var productname=$("#procucts_name").val();var serinum=$("#serinum").val();
    var productsize=$("#productsize").val();var material=$("#material").val();
    var unit=$("input[name='unit']:checked").val();
    var productstyle=$("#productstyle").val();
    var productcolor=$("#productcolor").val();var sex=$("input[name='sex']:checked").val();
    var productorgin=$("#productorgin").val();var retailprice=$("#retailprice").val();
    var productnote=$("#productnote").val();
    var updateproductdata={
        name:productname,serinum:serinum,size:productsize,
        material:material,unit:unit,style:productstyle,
        color:productcolor,sex:sex,orgin:productorgin,
        retailprice:retailprice,note:productnote
    };
    var newproducturl="修改商品接口";
    postAjax(updateproductdata,newproducturl, function () {

    })
}
//radio ware-operate
function radio_ware_operate(){
    $("input:radio[value='出库']").on("click",function(){
        $("#out_price").removeAttr("disabled");
        $("#in_price").attr("disabled","true");
    });
    $("input:radio[value='入库']").on("click",function(){
        $("#out_price").attr("disabled","true");
        $("#in_price").removeAttr("disabled");
    });
    $("input:radio[value='过库']").on("click",function(){
        $("#in_price").removeAttr("disabled");
        $("#out_price").removeAttr("disabled");
    });
}
//ware-operate event
function wareoperate(){
    var opera=$("input[name='ware-operate']:checked").val();
    var productsnum=$("#productsnum").val();
    var storagelocation=$("#storagelocation").val();
    var inprice=$("#in_price").val();var outprice=$("#out_price").val();
    var phone=$("#buyersphone").val();var buyers=$("#buyers").val();
    var address=$("#buyersaddress").val();
    var state=$("input[name='state']:checked").val();
   var  waredata={
        opera:opera,number:productsnum,storagelocation:storagelocation,
        inprice:inprice,outprice:outprice,phone:phone,buyers:buyers,
        address:address,state:state
    };
   var  wareurl="库存操作接口";
    postAjax(waredata,wareurl, function () {

    })
}
function warereset(){
    $("input[name='ware-operate']").eq(1).attr("checked","checked");
    $("input[name='ware-operate']").eq(0).removeAttr("checked");
    $("input[name='ware-operate']").eq(2).removeAttr("checked");
    $("input[name='ware-operate']").eq(1).click();
    $("#productsnum").val();$("#storagelocation").val();
    $("#in_price").val();$("#out_price").val();
    $("#buyersphone").val();$("#buyers").val();$("#buyersaddress").val();
    $("input[name='state']").eq(0).attr("checked","checked");
    $("input[name='state']").eq(1).removeAttr("checked");
    $("input[name='state']").eq(0).click();
}
//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null ;
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}
//展示图片
function showpic(files){
    var html="";
    for(var i=0;i<files.length;i++){
        var f=files[i];
        if(/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|BMP)$/.test(f.name)){
            var objUrl = getObjectURL(f);
            //预览图片
            if (objUrl) {
                html+='<div class="col-sm-12 col-xs-12 imgpreview">' +
                          '<img src=\''+objUrl+'\' class="img-responsive product_content_img">'+
                           '<div class="product_content_title">' +
                               f.name+
                           '</div>'+
                           '<div>' +
                                '<button type="submit" onclick="deleteimg(this)"' +
                                         'class="btn btn-primary btn-xs">删除</button>'+
                           '</div>'+
                     '</div>';
            }
        }
        if(/\.(rar)$/.test(f.name)){
            objUrl = getObjectURL(f);
            //预览图片
            if (objUrl) {
                html+='<div class="col-sm-12 col-xs-12 imgpreview">' +
                            '<img src="imgs/rar.png" class="img-responsive product_content_img">'+
                            '<div class="product_content_title">' +
                                     f.name+
                            '</div>'+
                            '<div>' +
                                    '<button type="submit" onclick="deleteimg(this)"' +
                                    'class="btn btn-primary btn-xs">删除</button>'+
                            '</div>'+
                        '</div>';
            }
        }
        if(/\.(zip)$/.test(f.name)){
            objUrl = getObjectURL(f);
            //预览图片
            if (objUrl) {
                html+='<div class="col-sm-12 col-xs-12 imgpreview">' +
                            '<img src="imgs/zip.png" class="img-responsive product_content_img">'+
                            '<div class="product_content_title">' +
                                 f.name+
                            '</div>'+
                            '<div>' +
                                '<button type="submit" onclick="deleteimg(this)"' +
                                'class="btn btn-primary btn-xs">删除</button>'+
                            '</div>'+
                      '</div>';
            }
        }
        if(/\.(txt)$/.test(f.name)){
            objUrl = getObjectURL(f);
            //预览图片
            if (objUrl) {
                html+='<div class="col-sm-12 col-xs-12 imgpreview">' +
                            '<img src="imgs/txt.png" class="img-responsive product_content_img">'+
                            '<div class="product_content_title">' +
                                        f.name+
                            '</div>'+
                            '<div>' +
                                '<button type="submit" onclick="deleteimg(this)"' +
                                 'class="btn btn-primary btn-xs">删除</button>'+
                            '</div>'+
                        '</div>';
            }
        }
    }
    document.getElementById('imgcontent').innerHTML+=html;
}
//删除图片
function deleteimg(a){
    $(a).parent().parent().remove()
}